package com.lhx.crud.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhx.crud.bean.Department;
import com.lhx.crud.bean.Employee;
import com.lhx.crud.bean.MSG;
import com.lhx.crud.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.net.BindException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {

    /*调用autowired*/
//    父类属性的注入
//  应用场景：父类或抽象类中存在公共依赖的bean时；
    @Autowired
    EmployeeService employeeService;

    //根据id查询信息
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public  MSG  getEmp(@PathVariable("id")Integer id){
        Employee employee= employeeService.getEmp(id);
        return  MSG.success().add("Emp",employee);


    }

    /**
     *  批量删除
     * @param
     * * @return
     */
    //删除信息
    @RequestMapping(value = "/emp/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public  MSG  delteEmp(@PathVariable("ids")String ids) {

        if(ids.contains("-")) {
            //分割
            List<Integer> list = new ArrayList<>();
            //用数组添加
            String[] ids_id = ids.split("-");

            //多个用遍历添加
            for (String id : ids_id) {

                list.add(Integer.parseInt(id));
            }
            employeeService.del_Emps(list);
            return MSG.success();


        } else {
            //字符转化单个
            Integer id = Integer.parseInt(ids);
            employeeService.del_Emp(id);
            return MSG.success();
        }
    }


    //校验信息
    @RequestMapping(value = "/check")
    @ResponseBody
    public  MSG  checkUser(@RequestParam("emp_name") String emp_name){
        //先进行用户名格式校验
        String regName ="(^[a-zA-Z0-9_-]{3,16}$)|(^[\u2E80-\u9FFF]{2,5})";
        if(!emp_name.matches(regName)){
            return  MSG.fal().add("va_msg","用户必须是3-16位数字和2到5位汉字");
        }else{
            boolean  b =employeeService.check(emp_name);
            if(!b){
                //失败
                return MSG.fal().add("va_msg","用户名重复");
            }else{
                //成功
                return MSG.success();
            }

        }

    }
    //保存信息
     @RequestMapping(value = "/emp",method = RequestMethod.POST)
     @ResponseBody
     //参数绑定
     public  MSG save_Emp(@Valid Employee employee, BindingResult result){
        //如果出现错误
        if (result.hasErrors()){
            //通过map保存属性
            Map<String,Object> map  = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError:errors){
                //封装
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
                //打印错误信息
                System.out.println("封装错误信息为"+fieldError.getDefaultMessage());
            }
            return  MSG.fal().add("errors",map);
        }else{
            //调用Employeeservice
            employeeService.save_Emp(employee);
            //返回msg的值
            return  MSG.success();

        }

     }



    //json数据
    //需导入jar包


    //查询
    @RequestMapping("/emps")
    @ResponseBody
    public MSG getEmpsWithJson(
            @RequestParam(value = "pn", defaultValue = "2") Integer pn  ){
        //设置页码初始大小                       //传入的页面大小
        PageHelper.startPage(pn,5);

        //判断获取bean
        List<Employee> emps  = employeeService.getAll();
        PageInfo page = new PageInfo(emps,5);
        //分页5个一页

        //添加页码信息
//        model.addAttribute("pageInfo", page);

        return MSG.success().add("pageInfo",page);

    }

//  @RequestMapping("/emps")                //pageNumber
//  @ResponseBody
//    public String  getEmps(@RequestParam(value = "pn", defaultValue = "1")Integer pn, Model model){
//         //设置页码初始大小                       //传入的页面大小
//          PageHelper.startPage(pn,5);
//
//          //判断获取bean
//        List<Employee> emps  = employeeService.getAll();
//          PageInfo page = new PageInfo(emps,5);
//                                            //分页5个一页
//          //添加页码信息
//          model.addAttribute("pageInfo", page);
//
//                return "list";
//    }
}
